package com.wuyou.securitydemo.component;

import cn.hutool.core.map.MapUtil;
import com.wuyou.sso.common.contant.AuthConstants;
import com.wuyou.sso.common.contant.TokenConstant;
import com.wuyou.securitydemo.domain.TokenVO;
import com.wuyou.securitydemo.userdetails.CustomizedUser;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * CustomizedTokenEnhancer
 *
 * @Date 2025/4/14 15:18
 * @Author 无忧
 */
@Component
public class CustomizedTokenEnhancer {

    public TokenVO enhance(
            TokenVO tokenVO, Authentication authentication) {

        final Map<String, Object> additionalInfo = MapUtil.newHashMap(6);
        CustomizedUser customizedUser =
                (CustomizedUser) authentication.getPrincipal();
        tokenVO.setUserId(customizedUser.getId());
        tokenVO.setUserName(customizedUser.getUsername());
        tokenVO.setDisplayName(customizedUser.getDisplayName());
        additionalInfo.put(TokenConstant.USER_ID, String.valueOf(customizedUser.getId()));
        additionalInfo.put(TokenConstant.USER_NAME, customizedUser.getUsername());
        additionalInfo.put(TokenConstant.DEPT_ID, String.valueOf(customizedUser.getDeptId()));
        additionalInfo.put(TokenConstant.MOBILE, customizedUser.getMobile());
        additionalInfo.put(TokenConstant.LICENSE, AuthConstants.PROJECT_LICENSE);
        additionalInfo.put(TokenConstant.ACTIVE, Boolean.TRUE);
        additionalInfo.put(TokenConstant.TENANT_ID, customizedUser.getTenantId());
        additionalInfo.put(TokenConstant.DISPLAY_NAME, customizedUser.getDisplayName());
        additionalInfo.put(TokenConstant.COMPANY_CODE, customizedUser.getCompanyCode());
        additionalInfo.put(TokenConstant.TENANT_NAME, customizedUser.getTenantName());
        additionalInfo.put(TokenConstant.TENANT_VO, customizedUser.getTenantVO());
        tokenVO.setAdditional(additionalInfo);
        return tokenVO;
    }

}
